clear all
drop _all
set mem 6500m
set more off
macro drop _all

cap cd "C:\Users\alson\Dropbox\BID_Quisqueya_conAlba\SUBMISSIONS\EHBsubmission\1ST ROUND REVISION\Replication package" 

log close _all
log using ecuador.log, replace

global table  Tables\
global data Data\


use "C:\Users\alson\Dropbox\BID_Quisqueya_conAlba\SUBMISSIONS\EHBsubmission\1ST ROUND REVISION\Replication package\Data\Ecuador.dta", clear


** PER COUNTRY TABLE


global rif_reg_measures PPVT0_stand bpi_ag_i_std


foreach var of global rif_reg_measures {

    local lab: variable label `var'  // Save variable label in local `lab'
   
    eststo `var'_reg: reg `var' female, robust
	



eststo `var'_rifreg: bootstrap, seed(1234) reps(1000):rifsureg `var' female, qs(10 25 50 75 90)

foreach k of numlist 10 25 50 75 90 {
scalar A`k' = [q`k'][female]
scalar B10 = sqrt(e(V)[1,1])
scalar B25 = sqrt(e(V)[3,3])
scalar B50 = sqrt(e(V)[5,5])
scalar B75 = sqrt(e(V)[7,7])
scalar B90 = sqrt(e(V)[9,9])
scalar I`k' = `k'
}

di "Pairwise test P10 vs P90"
test [q90]female=[q10]female
sca `var'_10_90 = r(p)

di "Pairwise test P10 vs P50"
test [q50]female=[q10]female
sca `var'_10_50 = r(p)

di "Pairwise test P50 vs P90"
test [q90]female=[q50]female
sca `var'_50_90 = r(p)


putdocx begin

putdocx paragraph

local footnotes: di "Ecuador. For `lab': 10th = 90th (p-value = " string(scalar(`var'_10_90), "%9.3f") "), 10th = 50th (p-value = " string(scalar(`var'_10_50), "%9.3f") "), 50th = 90th (p-value = " string(scalar(`var'_50_90), "%9.3f") ")"

putdocx text ("`footnotes'")

putdocx save domains_pvalues.docx, append


}


** Create the global

foreach var of global rif_reg_measures {

global results_means $results_means `var'_reg

global results_rifreg $results_rifreg `var'_rifreg

}
	lab var female "Mean"

	esttab $results_means using "$table\tableEcuador.tex", replace ///
    b(%8.3f) se(%8.3f) label ///
    title("Female-male. Ecuador") star(* 0.10 ** 0.05 *** 0.01) ///
    nocons noobs nonotes postfoot("") 

	lab var female "Female"

	
	esttab $results_rifreg using "$table\tableEcuador.tex", append fragment ///
    b(%8.3f) se(%8.3f) label ///
    star(* 0.10 ** 0.05 *** 0.01) ///
    nocons nonotes nonumbers nomtitles postfoot("\hline\hline") 


** CONTROLS FOR EQ. 3 AND EQ. 4


global controls region1 region7 region8 region11 region12 region17 race1 race2 race3 race4 race5 race6 year0 year1 year2 year3 year4 year5 kids


** EQUATION 3: inputs on female dummy and controls

global inputs  home11_w0_stand_rev

foreach var of global inputs {

    local lab: variable label `var'  // Save variable label in local `lab'
   
    eststo `var'_reg: reg `var' female ${controls}, robust



eststo `var'_rifreg: bootstrap, seed(1234) reps(1000):rifsureg `var' female ${controls}, qs(10 25 50 75 90)

foreach k of numlist 10 25 50 75 90 {
scalar A`k' = [q`k'][female]
scalar B10 = sqrt(e(V)[1,1])
scalar B25 = sqrt(e(V)[3,3])
scalar B50 = sqrt(e(V)[5,5])
scalar B75 = sqrt(e(V)[7,7])
scalar B90 = sqrt(e(V)[9,9])
scalar I`k' = `k'
}

di "Pairwise test P10 vs P90"
test [q90]female=[q10]female
sca `var'_10_90 = r(p)

di "Pairwise test P10 vs P50"
test [q50]female=[q10]female
sca `var'_10_50 = r(p)

di "Pairwise test P50 vs P90"
test [q90]female=[q50]female
sca `var'_50_90 = r(p)

di "Joint test"
test [q90]female=[q75]female=[q50]female=[q25]female=[q10]female

putdocx begin

putdocx paragraph

local footnotes: di "Ecuador. For `lab': 10th = 90th (p-value = " string(scalar(`var'_10_90), "%9.3f") "), 10th = 50th (p-value = " string(scalar(`var'_10_50), "%9.3f") "), 50th = 90th (p-value = " string(scalar(`var'_50_90), "%9.3f") ")"

putdocx text ("`footnotes'")

putdocx save home_pvalues.docx, append

}



	esttab home11_w0_stand_rev_reg using "$table\tableEcuador_inputs.tex", replace ///
    b(%8.3f) se(%8.3f) label ///
    title("Female-male. Ecuador") star(* 0.10 ** 0.05 *** 0.01) ///
    noobs drop(_cons ${controls}) nonotes postfoot("") 

	
	esttab home11_w0_stand_rev_rifreg using "$table\tableEcuador_inputs.tex", append fragment ///
    b(%8.3f) se(%8.3f) label ///
    star(* 0.10 ** 0.05 *** 0.01) ///
    nonotes nonumbers drop(_cons ${controls}) nomtitles postfoot("\hline\hline")


** EQUATION 4: outcomes on female, SES, HOME and their interactions

foreach var of global rif_reg_measures {

reg `var' female wi2_stand female_wi2_stand home11_w0_stand_rev female_home_w0 ${controls}, rob

outreg2 using $table\reg_output_interactions_ecu, bdec(3) br excel append label drop(${controls}) nor2 nocons addnote("Controls included. Controls include: mother’s race, number of children younger than 14 in the household, children’s age and region dummies.") noomitted

}


	** Prepare the data for the pooled regression: one indicator per country

pca PPVT0_stand bpi_ag_i_std
predict one_index,score
egen one_index_stand = std(one_index)
lab var one_index_stand "ECD index"

gen country = "Ecuador"

rename dadhome finhh

rename home11_w0_stand_rev home

keep one_index_stand country female finhh mothers_age_birth primary secondary tertiary wi2_stand home

append using $data\pool.dta

save $data\pool.dta, replace